package com.salesmanager.core.business.category.service;

import com.salesmanager.core.business.category.dao.CategoryDao;
import com.salesmanager.core.business.category.model.Category;
import com.salesmanager.core.business.category.model.CategoryCriteria;
import com.salesmanager.core.business.content.model.ImageContentFile;
import com.salesmanager.core.business.generic.exception.ServiceException;
import com.salesmanager.core.business.generic.service.SalesManagerEntityServiceImpl;
import com.salesmanager.core.constants.CacheConstants;
import com.salesmanager.core.modules.cms.product.CmsImageFileManager;
import com.salesmanager.core.utils.Base64;
import com.salesmanager.core.utils.TaobaoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("categoryService")

public class CategoryServiceImpl extends SalesManagerEntityServiceImpl<Long, Category> implements CategoryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryServiceImpl.class);

    protected CategoryDao categoryDao;

	@Autowired
	CmsImageFileManager cmsImageFileManager;

	@Autowired
	public CategoryServiceImpl(CategoryDao categoryDao) {
		super(categoryDao);
		
		this.categoryDao = categoryDao;
	}

    @Override
    public List<Category> list(CategoryCriteria criteria) throws ServiceException {
        return categoryDao.list(criteria);
    }

    @Override
    public List<Category> listByDepth(int depth) {
        List<Category> list = categoryDao.listByDepth(depth);
        for (Category c : list){
            c.getCategories().size();
        }
        return list;
    }

    @Override
    public Category getByCode(String code) throws ServiceException {
        return categoryDao.getByCode(code);
    }

    @Override
    public void saveOrUpdate(Category category) throws ServiceException {


        //save or update (persist and attach entities
        if(category.getId()!=null && category.getId()>0) {

            super.update(category);

        } else {

            super.save(category);

        }
    }

    @Override
    public void addChild(Category parent, Category child) throws ServiceException {
        if(child==null) {
            throw new ServiceException("Child category and merchant store should not be null");
        }

        try {

            if(parent==null) {

                //assign to root
                child.setParent(null);
                child.setDepth(0);
                //child.setLineage(new StringBuilder().append("/").append(child.getId()).append("/").toString());
                child.setLineage("/");

            } else {

                Category p = this.getById(parent.getId());//parent
                String lineage = p.getLineage();

                int depth = p.getDepth();//TODO sometimes null

                child.setParent(p);
                child.setDepth(depth+1);
                child.setLineage(new StringBuilder().append(lineage).append(p.getId()).append("/").toString());
            }

            update(child);
            StringBuilder childLineage = new StringBuilder();
            childLineage.append(child.getLineage()).append(child.getId()).append("/");
            List<Category> subCategories = categoryDao.listByLineage(childLineage.toString());

            //ajust all sub categories lineages
            if(subCategories!=null && subCategories.size()>0) {
                for(Category subCategory : subCategories) {
                    if(child.getId()!=subCategory.getId()) {
                        addChild(child, subCategory);
                    }
                }

            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public List<Category> listByParentId(long parentId) {
        Category category = categoryDao.getById(parentId);
        List<Category> children = category.getCategories();
        for (Category c : children){
            c.getCategories().size();
            for (Category cc : c.getCategories()){
                cc.getDescription();
            }
        }
        return children;
    }
}
